package jpsearchcore;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class JPSearchCore {

	private String identifier;
	private List<Person> modifiers;
	private List<Person> creators;
	private List<Publisher> publishers;
	private XMLGregorianCalendar creationDate;
	private XMLGregorianCalendar modifiedDate;
	private String description;
	private RightsDescription rightsDescription;
	private List<Source> sources;
	private List<String> keywords;
	private List<String> collectionLabel;
	private String title;
	private int preferredValue;
	private Rating rating;
	private OriginalImageIdentifier originalImageIdentifier;
	private GPSPositioning gpsPositioning;
	private List<RegionOfInterest> regionOfInterest;
	private int width;
	private int height;

	public JPSearchCore() {
		identifier = null;
		modifiers = new ArrayList<Person>();
		creators = new ArrayList<Person>();
		publishers = new ArrayList<Publisher>();		
		creationDate = dateToXMLGregorianCalendar(new Date());
		modifiedDate = dateToXMLGregorianCalendar(new Date());				
		description = null;
		sources=  new ArrayList<Source>();
		keywords = new ArrayList<String>();
		collectionLabel = new ArrayList<String>();
		title = null;
		preferredValue = -1;
		regionOfInterest = new ArrayList<RegionOfInterest>();
		width = 0;
		height = 0;
	}

	public String getIdentifier() {
		return identifier;
	}
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
	public List<Person> getModifiers() {
		return modifiers;
	}
	public void setModifiers(List<Person> modifiers) {
		this.modifiers.addAll(modifiers);
	}
	public List<String> getCollectionLabel() {
		return collectionLabel;
	}
	public void setCollectionLabel(List<String> collectionLabel) {
		this.collectionLabel = collectionLabel;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<Person> getCreators() {
		return creators;
	}
	public void setCreators(List<Person> creators) {
		this.creators.addAll(creators);
	}
	public List<Publisher> getPublishers() {
		return publishers;
	}
	public void setPublishers(List<Publisher> publishers) {
		this.publishers.addAll(publishers);
	}

	public XMLGregorianCalendar getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(XMLGregorianCalendar creationDate) {
		this.creationDate = creationDate;
	}
	public void setCreationDate(String creationDate) {
		this.creationDate = stringToXMLGregorianCalendar(creationDate);
	}

	public XMLGregorianCalendar getModifiedDate() {
		return modifiedDate;
	}
	public void setModifiedDate(XMLGregorianCalendar modifiedDate) {
		this.modifiedDate = modifiedDate;
	}
	public void setModifiedDate(String modifiedDate) {
		this.modifiedDate = stringToXMLGregorianCalendar(modifiedDate);
	}

	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public RightsDescription getRightsDescription() {
		return rightsDescription;
	}
	public void setRightsDescription(RightsDescription rightsDescription) {
		this.rightsDescription = rightsDescription;
	}
	public List<Source> getSource() {
		return sources;
	}
	public void setSource(List<Source> source) {
		this.sources.addAll(source);
	}
	public List<String> getKeywords() {
		return keywords;
	}
	public void setKeywords(List<String> keywords) {
		this.keywords.addAll(keywords);
	}
	public int getPreferredValue() {
		return preferredValue;
	}
	public void setPreferredValue(int preferredValue) {
		this.preferredValue = preferredValue;
	}
	public Rating getRating() {
		return rating;
	}
	public void setRating(Rating rating) {
		this.rating = rating;
	}
	public OriginalImageIdentifier getOriginalImageIdentifier() {
		return originalImageIdentifier;
	}
	public void setOriginalImageIdentifier(
			OriginalImageIdentifier originalImageIdentifier) {
		this.originalImageIdentifier = originalImageIdentifier;
	}
	public GPSPositioning getGpsPositioning() {
		return gpsPositioning;
	}
	public void setGpsPositioning(GPSPositioning gpsPositioning) {
		this.gpsPositioning = gpsPositioning;
	}
	public List<RegionOfInterest> getRegionOfInterest() {
		return regionOfInterest;
	}
	public void setRegionOfInterest(List<RegionOfInterest> regionOfInterest) {
		this.regionOfInterest.addAll(regionOfInterest);
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}

	public void dumpData(){
		System.out.println("ImageDescription");
		System.out.println("JPSearchCoreType:");
		System.out.println("Identifier: " + identifier);

		System.out.println("Modifiers: " + modifiers);

		System.out.println("Creators: " + creators);

		for(Publisher p : publishers){
			System.out.println(p);
		}		

		System.out.println("Creation Date: " + creationDate);
		System.out.println("Modified Date: " + modifiedDate);
		System.out.println("Description: " + description);

		System.out.println(rightsDescription);

		for(Source s : sources){
			System.out.println(s);
		}

		System.out.print("Keywords: [");
		for(String s : keywords){
			System.out.print(s + " ");
		}
		System.out.println("]");

		System.out.println("Title: " + title);
		System.out.println("CollectionLabel: " + collectionLabel);

		System.out.println("Preferred Value: " + preferredValue);

		System.out.println(rating);

		System.out.println(originalImageIdentifier);

		System.out.println(gpsPositioning);

		for(RegionOfInterest r : regionOfInterest){
			System.out.println(r);
		}			

		System.out.println("Width: "+ width);
		System.out.println("Height: "+ height);
	}

	public static XMLGregorianCalendar stringToXMLGregorianCalendar(String sDate){

		if(sDate != null && !sDate.equals("")){
			XMLGregorianCalendar xmlGC = null;
			try {
				xmlGC = DatatypeFactory.newInstance().newXMLGregorianCalendar(sDate);				
			} catch (DatatypeConfigurationException e) { //in case of converting date problem
				e.printStackTrace();
				xmlGC = dateToXMLGregorianCalendar(new Date());
			}
			
			return xmlGC;
		}	
		return null;
	}

	public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date){

		GregorianCalendar c = new GregorianCalendar();
		c.setTime(new Date());
		XMLGregorianCalendar date2 = null;
		try {
			date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}	
		return date2;
	}

}
